package com.wanmait.hotelManagement.servlet;

import java.io.IOException;
import java.io.Serializable;
import java.lang.ProcessBuilder.Redirect;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManagement.dao.OrderDao;
import com.wanmait.hotelManagement.dao.RoomTypeDao;
import com.wanmait.hotelManagement.vo.Order;
import com.wanmait.hotelManagement.vo.OrderDetail;
import com.wanmait.hotelManagement.vo.Passenger;
import com.wanmait.hotelManagement.vo.RoomType;

/**
 * Servlet implementation class ReservationServlet
 */
@WebServlet("/ReservationServlet")
public class ReservationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if("show".equals(action))
		{
			this.show(request, response);
		}
		else if("submitInfo".equals(action))
		{
			this.submitInfo(request, response);
		}
	}

	protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(request.getParameter("roomTypeId"));
		RoomType roomType = RoomTypeDao.getDao().getRoomTypeById(id);
		request.setAttribute("roomType", roomType);
		request.getRequestDispatcher("/hotel/reservation/reservation.jsp").forward(request, response);
	}
	
	protected void submitInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/* SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd"); */
		int roomTypeId = Integer.parseInt(request.getParameter("roomTypeId"));
		//////////////////////////////////////////////////////
		int passengerId = Integer.parseInt(request.getParameter("passengerId"));
		///////////////////////////////////////////////
		int roomCount = Integer.parseInt(request.getParameter("roomCount"));
		String arriveDate = request.getParameter("arriveDate");
		String[] strs = arriveDate.split("/");
		arriveDate = strs[2]+"-"+strs[0]+"-"+strs[1]+" 00:00:00";
		/* date.format(arriveDate); */
		/* System.out.println(date.format(arriveDate)); */
		String leaveDate = request.getParameter("leaveDate");
		String[] strss = leaveDate.split("/");
		leaveDate = strss[2]+"-"+strss[0]+"-"+strss[1]+" 00:00:00";
		/* date.format(leaveDate); */
		
		/* String orderNumber = OrderDao.getDao().getOrderNumber(); */
		String remarks = request.getParameter("remarks");
		Order order = new Order();
		/* order.setOrderNumber(orderNumber); */
		RoomType roomType = new RoomType();
		roomType.setId(roomTypeId);
		order.setRoomTypeId(roomType);
		////////////////////
		Passenger passenger = new Passenger();
		passenger.setId(passengerId);
		order.setPassengerId(passenger);
		////////////////////////
		order.setRoomCount(roomCount);
		
		order.setArriveDate(Timestamp.valueOf(arriveDate));
		
		order.setLeaveDate(Timestamp.valueOf(leaveDate));
		order.setRemarks(remarks);
		OrderDao.getDao().submitOrder(order);
		
		int id = OrderDao.getDao().getOrderId();
		if(id!=0)
		{
			String[] liveNames = request.getParameterValues("liveName");
			for(String liveName:liveNames)
			{
				OrderDetail orderDetail = new OrderDetail();
				Order orderId = new Order();
				orderId.setId(id);
				orderDetail.setOrderId(orderId);
				orderDetail.setLiveName(liveName);
				OrderDao.getDao().submitOrderDetail(orderDetail);
			}
			String redirect = request.getHeader("referer");
			response.sendRedirect(redirect+"&submit=success");
		}
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
